package com.wx_shop.api.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.wx_shop.api.mapper.SysAuthorityMapper;
import com.wx_shop.api.mapper.SysUserMapper;
import com.wx_shop.api.mapper.SysUserRoleRelMapper;

@Component
public class MyUserDetailsService implements UserDetailsService {

	private static final Logger logger = LoggerFactory.getLogger(MyUserDetailsService.class);
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysAuthorityMapper sysAuthorityMapper;
	
	@Autowired
	private SysUserRoleRelMapper sysUserRoleRelMapper;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		logger.info("username={}", username);
		
		// api 接口请求
		if( ApiAuthenticationToken.API_PRINCIPAL.equals(username) ) { 
			
			return new LoginUser(ApiAuthenticationToken.API_PRINCIPAL, 
					ApiAuthenticationToken.API_CREDENTIALS, true, true, true, true, null);
			
		} else {
			// 页面登录请求
			
			return null;
		}
	}
}
